package cn.javaxc.mvcwork.control.web;
import cn.javaxc.mvcwork.util.common.StringUtils;
import java.util.*;


/**
 * 通过一个数据列表,构建页面的引用框,下拉框,单选框,多选框....等
 *
 * @mvcversion 1.0
 *
 */


public class HtmlSelect extends ArrayList {

  private String selectName = "";
  private int selectSize = 1;
  private String selectStyle = null;

  private String firstEntry = "";
  private String firstKey = "";

  private String defaultKey = "";
  private String defaultValue = "";
  private String jsEvent = null;
  private boolean multiple = false;
  private boolean disabled = false;
  private int processedRowCount = 0;
  private boolean checkboxOutput = false;

  private StringBuffer rowList = new StringBuffer();

  private boolean built = false;
  private List<String> multipleSelects = null;
  private String idName = null;
  private HashMap attributeList = new HashMap();
  private String checktag="radio";

  

  public HtmlSelect() {
  }


  
  public HtmlSelect(ArrayList itemsToAdd) {
    Iterator i = itemsToAdd.iterator();
    while (i.hasNext()) {
      String thisItem = (String) i.next();
      this.addItem(thisItem);
    }
  }


  
  public HtmlSelect(Map itemsToAdd) {
    Iterator keys = itemsToAdd.keySet().iterator();
    Iterator values = itemsToAdd.values().iterator();
    while (keys.hasNext() && values.hasNext()) {
      Integer key = (Integer) keys.next();
      String value = (String) values.next();
      this.addItem(key.intValue(), value);
    }
  }


  
  public boolean getDisabled() {
    return disabled;
  }


  
  public void setDisabled(boolean tmp) {
    this.disabled = tmp;
  }


  
  public void setSelectName(String tmp) {
    this.selectName = tmp;
  }


  
  public void setDefaultValue(String tmp) {
    if (tmp != null) {
      if (!tmp.equals(defaultValue)) {
        built = false;
      }
    }
    if (tmp == null) {
      this.defaultValue = "";
    } else {
      this.defaultValue = tmp;
    }
  }


  public void setMultiple(boolean multiple) {
    this.multiple = multiple;
	if (multiple){
		if (checkboxOutput){
			checktag="checkbox";
		}
	}
  }


  
  public void setDefaultValue(int tmp) {
    this.defaultValue = "" + tmp;
  }


  
  public void setDefaultKey(String tmp) {
    if (tmp != null) {
      if (!tmp.equals(defaultKey)) {
        built = false;
      }
      if (defaultKey != null) {
        this.defaultKey = tmp;
      } else {
        this.defaultKey = "";
      }
    }
  }


  
  public void setDefaultKey(int tmp) {
    setDefaultKey(String.valueOf(tmp));
  }


  
  public void setSelectSize(int tmp) {
    this.selectSize = tmp;
  }


  
  public void setSelectStyle(String tmp) {
    this.selectStyle = tmp;
  }


  
  public int getSelectSize() {
    return selectSize;
  }


  
  public void setBuilt(boolean built) {
    this.built = built;
  }


  
  public boolean getBuilt() {
    return built;
  }


  
  public String getIdName() {
    return idName;
  }


  
  public void setIdName(String idName) {
    this.idName = idName;
  }


  
  public void setFirstEntry(String tmp) {
    this.firstEntry = tmp;
  }


  
  public void setFirstEntry(String tmp1, String tmp2) {
    this.firstKey = tmp1;
    this.firstEntry = tmp2;
  }


  
  public void setCheckboxOutput(boolean tmp) {
    checkboxOutput = tmp;
  }



  
  public void setTypeLocale() {
    Locale[] locales;
    locales = Locale.getAvailableLocales();
    for (int i = 0; i < locales.length; i++) {
      if (locales[i].getCountry().length() > 0) {
        this.addItem(i, locales[i].getDisplayName());
      }
    }
  }


  
  public List<String> getMultipleSelects() {
    return multipleSelects;
  }


  
  public void setMultipleSelects(List<String> multipleSelects) {
    this.multipleSelects = multipleSelects;
  }


  
  public void setJsEvent(String tmp) {
    this.jsEvent = tmp;
  }


  
  public void addAttribute(String attrName, String attrValue) {
    attributeList.put(attrName, attrValue);
  }


  
  public boolean getMultiple() {
    return multiple;
  }


  
  public String getAttributeList() {
    StringBuffer tmpList = new StringBuffer();
    Set s = attributeList.keySet();
    Iterator i = s.iterator();
    tmpList.append(" ");
    while (i.hasNext()) {
      Object name = i.next();
      tmpList.append(
          name.toString() + "=\"" + attributeList.get(name).toString() + "\" ");
    }
    return tmpList.toString();
  }


  
  public String getHtml(String thisSelectName) {
    selectName = thisSelectName;
    if (!built) {
      build();
    }
    return getHtml();
  }


  
  public String getHtml(String thisSelectName, String thisDefaultValue) {
    selectName = thisSelectName;
    setDefaultValue(thisDefaultValue);
    if (!built) {
      build();
    }
    return getHtml();
  }


  
  public String getHtml(String thisSelectName, int thisDefaultKey) {
    selectName = thisSelectName;
    setDefaultKey(thisDefaultKey);
    if (!built) {
      build();
    }
    return getHtml();
  }


  
  public String getHtml() {
    //Build the html for the results
    if (!built) {
      build();
    }
    StringBuffer outputHtml = new StringBuffer();
    if (!checkboxOutput) {
      outputHtml.append(
          "<select size='" + this.selectSize + "' " +
          (selectStyle == null ? "" : "style='" + selectStyle + "' ") +
          "name='" + this.selectName + "'" +
          (jsEvent != null ? " " + this.jsEvent : "") +
          (idName != null ? " " + "id='" + this.idName + "' " : "") +
          (multiple ? " multiple " : "") +
          (disabled ? " disabled " : "") +
          this.getAttributeList() + ">");
    }else{
//		outputHtml.append("<span class='icheck'>");
	}

    //Process the rows
    outputHtml.append(rowList.toString());

    if (!checkboxOutput) {
      outputHtml.append("</select>");
    }else{
//		outputHtml.append("</span>");
	}

    return (outputHtml.toString());
  }


  
  public String getJsEvent() {
    return jsEvent;
  }


  
  public String toString() {
    return this.getHtml();
  }


  
  public void addGroup(String category) {
    HtmlOption thisGroup = new HtmlOption();
    thisGroup.setGroup(true);
    thisGroup.setText(category);
    this.add(thisGroup);
  }


  
  protected void addItem(String tmp1, String tmp2, int position) {
    this.add(position, new HtmlOption(tmp1, tmp2));
  }


  
  public void addItem(int tmp1, String tmp2, HashMap attributes) {
    this.add(new HtmlOption(String.valueOf(tmp1), tmp2, attributes));
  }


  
  public void addItem(int tmp1, String tmp2, HashMap attributes, boolean enabled) {
    this.add(new HtmlOption(String.valueOf(tmp1), tmp2, attributes, enabled));
  }


  
  public void addItem(String tmp1, String tmp2) {
    this.add(new HtmlOption(String.valueOf(tmp1), tmp2));
  }


  
  public void addItem(String tmp) {
    this.add(new HtmlOption(String.valueOf(tmp), String.valueOf(tmp)));
  }


  
  public void addItem(int tmp1, String tmp2) {
    this.add(new HtmlOption(String.valueOf(tmp1), tmp2));
  }

  public void addItems(HtmlSelect items)
  {
	    Iterator i = items.iterator();
	    while (i.hasNext()) {
	    	HtmlOption thisItem = (HtmlOption) i.next();
	      this.add(thisItem);
	    }
  }

  
  public void addItem(int tmp1, String tmp2, int position) {
    this.add(position, new HtmlOption(String.valueOf(tmp1), tmp2));
  }


 

  
  public void build() {
    rowList.setLength(0);
    built = true;
    int rowSelect = -1;
    //If a default has not been set, then default the list to the first entry
    if (!multiple&&this.defaultValue.equals("") && this.defaultKey.equals("")) {
      rowSelect = 1;
    }
    //Check to see if an entry needs to be added before the rows are processed and whether it should be default
    if (!this.firstEntry.equals("")) {
      if (((this.defaultValue.equals("")) && (this.defaultKey.equals(""))) ||
          (this.defaultValue.equals(this.firstEntry)) ||
          (this.defaultKey.equals(this.firstKey))) {
        rowSelect = -1;
        if (!checkboxOutput) {
          rowList.append(
              "<option selected value='" + 0 + "'>" + this.firstEntry + "</option>");
        } else {
          rowList.append(
              toHtml("<input type='"+checktag+"' checked class=ace>" , this.firstEntry));
        }
      } else {
        if (!checkboxOutput) {
          rowList.append(
              "<option value='" + 0 + "'>" + this.firstEntry + "</option>");
        } else {
          rowList.append(toHtml("<input type='"+checktag+"' class=ace>", this.firstEntry));
        }
      }
    }

    //Process a Vector
    boolean groupOpen = false;
    for (int i = 0; i < this.size(); i++) {
      ++processedRowCount;
      HtmlOption thisOption = (HtmlOption) this.get(i);
      String tmp1 = thisOption.getValue();
      String tmp2 = thisOption.getText();
	  if(tmp2==null)tmp2=tmp1;
      String attributes = thisOption.getAttributes();
      if (thisOption.isGroup()) {
        if (groupOpen) {
          rowList.append("</optgroup>");
        }
        rowList.append("<optgroup label=\"" + tmp2 + "\">");
      } else {
        String optionChecked = "";
        String optionSelected = "";
        if (multipleSelects != null && multipleSelects.contains(tmp1)) {
          optionSelected = "selected ";
          optionChecked = " checked";
        } else if (multipleSelects == null && (tmp2.equals(this.defaultValue) ||
            tmp1.equals(this.defaultValue) ||
            rowSelect == processedRowCount ||
            tmp1.equals(this.defaultKey))) {
          optionSelected = "selected ";
          optionChecked = " checked";
        }
        //Build the option row
        if (!checkboxOutput) {
          rowList.append(
              "<option " + optionSelected + "value='" + tmp1 + "' " + attributes + ">" + StringUtils.toHtmlValue(
              tmp2) + "</option>");
        } else {
          rowList.append(
              toHtml("<input type='"+checktag+"'" + optionChecked +" name='" + this.selectName +"' value='" + tmp1 + "'  class=ace " +(jsEvent != null ? " " + this.jsEvent : "")+ ">", tmp2));
        }
      }
    }
    if (groupOpen) {
      rowList.append("</optgroup>");
    }
  }



 
  
  private String toHtml(String input,String s) {
	return "<label>"+input+"<span class='lbl'>"+s+"</span></label>";
  }

  
  public String getValueFromId(String key) {
    Iterator i = this.iterator();
    while (i.hasNext()) {
      HtmlOption thisOption = (HtmlOption) i.next();
      if (key.equals(thisOption.getValue())) {
        return thisOption.getText();
      }
    }
    return null;
  }


  
  public boolean hasKey(String key) {
    Iterator i = this.iterator();
    while (i.hasNext()) {
      HtmlOption thisOption = (HtmlOption) i.next();
      if (key.equals(thisOption.getValue())) {
        return true;
      }
    }
    return false;
  }

  public boolean hasText(String text) {
    Iterator i = this.iterator();
    while (i.hasNext()) {
      HtmlOption thisOption = (HtmlOption) i.next();
      if (text.equals(thisOption.getText())) {
        return true;
      }
    }
    return false;
  }
}
